# -*- coding: utf-8 -*-

from app.foundation import exception_logger, server_logger
import time


def command_log(func):
    def decorate(*args, **kwargs):
        command_name = func.__name__
        server_logger.info("[%s]开始:" % (command_name,))
        try:
            func(*args, **kwargs)
        except Exception as e:
            exception_logger.error(str(e))
            raise e
        server_logger.info("[%s]结束" % (command_name,))

    return decorate


def freq_limit(seconds):
    timer = {}

    def wrapper(func):
        func_name = func.__name__

        def decorate(*args, **kwargs):
            if func_name in timer.keys():
                last_time = timer[func_name]
                now_time = time.time()
                if seconds > now_time - last_time:
                    time.sleep(seconds)
            func(*args, **kwargs)
            timer[func_name] = time.time()

        return decorate

    return wrapper
